module CanCamel::Filters
  class Base
    def initialize(*)
    end

    def name
      self.class.to_s.demodulize.underscore.to_sym
    end

    def catch_args
      nil
    end

    def catch_result
      []
    end

    def filter(**_)
      {}
    end

    def lint!
      raise CanCamel::Linter::LintingError if (catch_args & catch_result).try(:any?)
    end

    def invoke!(args: {}, result: {})
      args = my_slice(args, catch_args) if catch_args
      args.merge! my_slice(result, catch_result) { |l, r, _k| r || l }
      filter(**args)
    end

    def validates(*args)
      CanCamel::Validators.validates(name, *args)
    end

    private

    def my_slice(hash, args)
      args.each_with_object({}) { |x, object| object[x] = hash[x]  }
    end
  end
end
